package com.emm.yixun.common.utils;


import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 第一信息短信网关的初级封装
 */
public final class SMSUtil {// 短信接口调用地址
	private String smsurl;
	// 用户名
	private String userName;
	// 密码
	private String pwd;

	private String sign;

	private enum SMSError {
		MGCH_ERROR(1, "含有敏感词汇"), YE_ERROR(2, "余额不足"), MOBILE_ERROR(3, "没有号码"), SQL_ERROR(
				4, "包含sql语句"), ACCOUNT_ERROR(10, "账号不存在"), NO_ACCOUNT_ERROR(11,
				"账号注销"), STOP_ACCOUNT_ERROR(12, "账号停用"), IP_ERROR(13, "IP鉴权失败"), FORMAT_ERROR(
				14, "格式错误"), SYSTEM_ERROR(-1, "系统异常"), WL_ERROR(-999, "网络异常"), JX_ERROR(
				-998, "返回参数解析错误");

		private int code;
		private String msg;

		SMSError(int code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		private static String getMsg(int code) {
			for (SMSError item : SMSError.values()) {
				if (item.code == code) {
					return item.msg;
				}
			}
			throw new IllegalArgumentException(
					"can not find error message for code:" + code);
		}
	}

	public static String getMsg(int code) {
		return SMSError.getMsg(code);
	}

	public int sendSMS(String content, String mobile) {
		// 签名
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("name", userName));
		params.add(new BasicNameValuePair("pwd", pwd));
		params.add(new BasicNameValuePair("mobile", mobile));
		params.add(new BasicNameValuePair("content", content));
		params.add(new BasicNameValuePair("sign", sign));
		params.add(new BasicNameValuePair("stime", ""));
		params.add(new BasicNameValuePair("type", "pt"));
		params.add(new BasicNameValuePair("extno", ""));
		HttpClient httpClient = HttpClientBuilder.create().build();

		String queryString = URLEncodedUtils.format(params, "UTF-8");
		String url = smsurl + "?" + queryString;
		HttpPost httpGet = new HttpPost(url);

		try {
			HttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String data = EntityUtils.toString(response.getEntity());
				String status = data.split(",")[0];
				return Integer.parseInt(status);
			} else {
				return -999;
			}
		} catch (IOException e) {
			// 发生网络异常
			return -999;
		} catch (NumberFormatException e) {
			// 返回值不是有效的数字
			return -998;
		}
	}

	public int sendSMS(String content, String[] mobiles) {
		for (String mobile : mobiles) {
			sendSMS(content, mobile);
		}
		return 0;
	}

	public String getSmsurl() {
		return smsurl;
	}

	public void setSmsurl(String smsurl) {
		this.smsurl = smsurl;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public SMSUtil() {
		super();
	}

	public SMSUtil(String smsurl, String userName, String pwd, String sign) {
		super();
		this.smsurl = smsurl;
		this.userName = userName;
		this.pwd = pwd;
		this.sign = sign;
	}
}
